package com.branik.delivery.managed.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.springframework.dao.DataAccessException;

import com.branik.delivery.common.Constants;
import com.branik.delivery.model.Pizza;
import com.branik.delivery.service.IPizzaService;

/**
 * 
 * Pizza Managed Bean
 * 
 * @author EBraga
 * @version 1.0.0
 *
 */
@ManagedBean(name="pizzaMB")
@RequestScoped
public class PizzaManagedBean extends GenericManagedBean {

	private static final long serialVersionUID = 5200878225424820722L;

	//Spring Pizza Service is injected...
	@ManagedProperty(value="#{PizzaService}")
	IPizzaService pizzaService;
	
	List<Pizza> pizzaList;
	
	private int id;
	private String name;
	private String surname;
	

	/**
	 * Add Pizza
	 * 
	 * @return String - Response Message
	 */
	public String addPizza() {
		try {
			Pizza pizza = new Pizza();
			pizza.setId(getId());
			pizza.setName(getName());
			pizza.setSurname(getSurname());
			getPizzaService().addPizza(pizza);
			return Constants.REDIRECT.SUCCESS;
		} catch (DataAccessException e) {
			e.printStackTrace();
		} 	
		
		return Constants.REDIRECT.ERROR;
	}
	
	/**
	 * Reset Fields
	 * 
	 */
	public void reset() {
		this.setId(0);
		this.setName("");
		this.setSurname("");
	}
	
	/**
	 * Get Pizza List
	 * 
	 * @return List - Pizza List
	 */
	public List<Pizza> getPizzaList() {
		pizzaList = new ArrayList<Pizza>();
		pizzaList.addAll(getPizzaService().getPizzas());
		return pizzaList;
	}
	
	/**
	 * Get Pizza Service
	 * 
	 * @return IPizzaService - Pizza Service
	 */
	public IPizzaService getPizzaService() {
		return pizzaService;
	}

	/**
	 * Set Pizza Service
	 * 
	 * @param IPizzaService - Pizza Service
	 */
	public void setPizzaService(IPizzaService pizzaService) {
		this.pizzaService = pizzaService;
	}
	
	/**
	 * Set Pizza List
	 * 
	 * @param List - Pizza List
	 */
	public void setPizzaList(List<Pizza> pizzaList) {
		this.pizzaList = pizzaList;
	}
	
	/**
	 * Get Pizza Id
	 * 
	 * @return int - Pizza Id
	 */
	public int getId() {
		return id;
	}

	/**
	 * Set Pizza Id
	 * 
	 * @param int - Pizza Id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Get Pizza Name
	 * 
	 * @return String - Pizza Name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Set Pizza Name
	 * 
	 * @param String - Pizza Name
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * Get Pizza Surname
	 * 
	 * @return String - Pizza Surname
	 */
	public String getSurname() {
		return surname;
	}
	
	/**
	 * Set Pizza Surname
	 * 
	 * @param String - Pizza Surname
	 */
	public void setSurname(String surname) {
		this.surname = surname;
	}	
	
}